<template>
	<view class="pages">
		<u-navbar title="组织" :autoBack="true" placeholder bgColor="#fff"></u-navbar>
		<view class="top-search">
      <u-search
        placeholder="搜索组织名称"
        @search="search"
        :showAction="false"
      ></u-search>
		</view>
    <u-line></u-line>
    <view>
      <u-cell v-for="(item, index) in list" :key="index" :name="item.join(',')" :title="item[1]" :value="item[2] || ''" isLink @click="selectOrg"></u-cell>
    </view>
	</view>
</template>

<script>
import { getMaterialInformation } from '@/api/index'
import { ENV } from '../../.env.js'

export default {
  data() {
    return {
      type: '',
      token: '',
      orgList: [],
      list: [],
      orgInfo: {},
    }
  },
  onLoad(options) {
    if(options.token) {
      this.token = options.token
    }
    if(options.type) {
      this.type = options.type
      this.orgInfo = uni.getStorageSync('orgInfo')
    }
    this.getPage()
  },
  methods: {
    getPage() {
      uni.showLoading({ mask: true });
      const parameters = [
        {"FormId":"ORG_Organizations","TopRowCount":0,"Limit":1000,"StartRow":0,"FilterString":"","OrderString":"FNumber ASC","FieldKeys":"FNumber,FName"}
      ]
      let header = {}
      if(this.token) {
        header[ENV.HEAD] = this.token
      }
      getMaterialInformation({parameters}, header).then(res => {
        uni.hideLoading();
        this.orgList = res
        if(this.type=='2') {
          this.orgList.forEach(v => {
            if(v[0] == this.orgInfo.orgId) {
              v.push('当前登录组织')
            }
          })
        }
        this.list = res
      })
    },
    search(e) {
      if(e) {
        this.list = this.orgList.filter(v => v[1].includes(e))
      } else {
        this.list = [...this.orgList]
      }
    },
    selectOrg(e) {
      const that = this
      const info = e.name.split(',')
      if(this.type == '1') {
        this.jumpTo('/pages/inventoryQuery/inventoryQuery', {orgId: info[0]})
        return
      }
      if(this.type == '2') {
        uni.setStorageSync('orgInfo', {orgName: info[1], orgId: info[0]})
        uni.showToast({
          title: '组织切换成功',
          icon: "none",
        });
        setTimeout(function() {
          that.reLaunch('/pages/index/index')
        }, 1500)
        return
      }
      if(this.type == '3') {
        this.jumpTo('/pages/inventoryQuery/goodsDetail', {orgId: info[0]})
        return
      }
      if(this.type == '4') {
        this.jumpTo('/pages/reportForms/reportForm', {orgId: info[0]})
        return
      }
      uni.setStorageSync('token', this.token)
      uni.setStorageSync('orgInfo', {orgName: info[1], orgId: info[0]})
      this.reLaunch('/pages/index/index')
    },
  }
}
</script>

<style lang="scss" scoped>
.pages{
  width: 750rpx;
  min-height: 100vh;
  padding-bottom: 40rpx;
  .top-search {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 30rpx 20rpx;
    margin-top: 20rpx;
  }
}
</style>